const app = getApp()
Page({
    data: {
        token: wx.getStorageSync('token'),
        isShowConfirm: false,
        overage: null,
        showUserInfoModel: false,
        showPasswordModel: false,
        existencePassword: false,
        oldPassword: '',
        newPassword: '',
        confirm_password: '',
        carNumber: '',
        tel: '',
        stationType: 1,
        isRadio: 1,
    },
    onLoad: function () {

    },
    onShow: function () {
        const that = this
        let token = that.data.token
        if (!token) {
            token = wx.getStorageSync('token')
            if (token) {
                that.setData({token: token})
            } else {
                wx.showToast({
                    title: "操作太快了，下拉刷新页面",
                    icon: 'none',
                    duration: 2000
                })
            }
        }
        if (app.globalData.userInfo) {
            if (app.globalData.userInfo.password === 'yes') {
                that.setData({existencePassword: true})
            } else {
                that.setData({existencePassword: false})
            }
        } else {
            app.getUserInfoByToken(that.data.token)
        }
    },
    /**
     * 点击设置触发
     */
    changeView: function () {
        wx.openSetting({
            success: (res) => {
                if (res.authSetting["scope.userInfo"] === false) {
                    wx.navigateTo({
                        url: '../login/login',
                    })
                }
            }
        })
    },
    /**
     * 获取输入余额
     */
    getOverage: function (e) {
        let value = e.detail.value;
        value = value.replace(/[^\d.]|^\./g, '').replace(/\.{2}/g, '.').replace(/^([1-9]\d*|0)(\.\d{1,2})(\.|\d{1})?$/, '$1$2').replace(/^0\d{1}/g, '0');
        this.setData({
            overage: value
        })
    },
    /**
     * 我的信息
     */
    myInfo: function () {
        const userInfo = app.globalData.userInfo
        if (userInfo) {
            this.setData({
                showUserInfoModel: true,
                carNumber: userInfo.carNumber,
                tel: userInfo.tel,
                stationType: userInfo.stationType,
                isRadio: userInfo.stationType,
            })
        } else {
            this.setData({
                showUserInfoModel: true,
                carNumber: '',
                tel: '',
                stationType: '',
            })
        }
    },
    /**
     * 我的钱包
     */
    myWallet: function () {
        const that = this
        if (!that.data.token) {
            wx.showToast({
                title: "操作太快了，请稍后重试",
                icon: 'none',
                duration: 2000
            })
            app.getToken()
            return;
        }
        wx.request({
            url: app.globalData.baseurl + "user/getOverage",
            method: "POST",
            data: {token: that.data.token},
            success(res) {
                if (res.data.code === 200) {
                    wx.showModal({
                        title: '我的钱包',
                        content: '剩余金额：' + res.data.data + '元',
                        showCancel: true,
                        confirmText: '充值',
                        cancelText: '取消',
                        success: (res) => {
                            if (res.confirm) {
                                that.setData({
                                    isShowConfirm: true
                                })
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    /**
     * 跳转停车记录
     */
    parkingRecord: function () {
        wx.switchTab({url: '../order/order'})
    },
    /**
     * 修改密码
     */
    updatePassword: function () {
        this.setData({
            showPasswordModel: true,
            oldPassword: '',
            newPassword: '',
            confirm_password: '',
        })
    },
    /**
     * 取消修改密码
     */
    cancelPassword: function () {
        this.setData({showPasswordModel: false})
    },
    /**
     * 取消修改信息
     */
    cancelUserInfo: function () {
        this.setData({showUserInfoModel: false})
    },
    /**
     * 确认修改密码
     */
    confirmPassword: function () {
        const that = this
        const temp = {
            token: that.data.token,
            newPassword: that.data.newPassword,
        }
        if (that.data.existencePassword) {
            if (that.data.oldPassword.length === 0) {
                wx.showToast({
                    title: '请输入旧密码',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
            if (that.data.oldPassword === that.data.newPassword) {
                wx.showToast({
                    title: '旧密码和新密码一致',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
            temp.oldPassword = that.data.oldPassword
        }
        if (that.data.newPassword.length === 0) {
            wx.showToast({
                title: '请输入新密码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.newPassword !== that.data.confirm_password) {
            wx.showToast({
                title: '两次密码不相等',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        this.setData({showPasswordModel: false})
        wx.request({
            url: app.globalData.baseurl + "user/updatePassword",
            method: "POST",
            data: temp,
            success: function (res) {
                app.getUserInfoByToken(that.data.token)
                if (res.data.code === 200) {
                    that.setData({existencePassword: true})
                }
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 确认修改信息
     */
    confirmUserInfo: function () {
        const that = this
        if (that.data.carNumber.length === 0) {
            wx.showToast({
                title: '请输入车牌号',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.tel.length === 0) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.stationType.length === 0) {
            wx.showToast({
                title: '请选择车型',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        this.setData({showUserInfoModel: false})
        wx.request({
            url: app.globalData.baseurl + "user/updateUserByToken",
            data: {
                token: that.data.token,
                carNumber: that.data.carNumber,
                tel: that.data.tel,
                stationType: that.data.stationType,
            },
            success: function (res) {
                app.getUserInfoByToken(that.data.token)
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            },
            method: "POST"
        })
    },
    /**
     * 取消隐藏
     */
    cancel: function () {
        const that = this
        that.setData({
            isShowConfirm: false,
        })
    },
    /**
     * 确认操作
     */
    confirmAcceptance: function () {
        const that = this
        that.setData({
            isShowConfirm: false,
        })
        if (!that.data.overage) {
            wx.showToast({
                title: "请输入金额",
                icon: 'none',
                duration: 2000
            })
            return;
        }
        wx.request({
            url: app.globalData.baseurl + "user/addOverage",
            method: "POST",
            data: {
                token: that.data.token,
                overage: that.data.overage,
            },
            success(res) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
                that.setData({
                    overage: null,
                })
            }
        })
    },
    /**
     * 获取输入旧密码
     */
    getOldPassword: function (e) {
        this.setData({oldPassword: e.detail.value.trim()})
    },
    /**
     * 获取输入新密码
     */
    getNewPassword: function (e) {
        this.setData({newPassword: e.detail.value.trim()})
    },
    /**
     * 获取输入确认密码
     */
    getConfirmPassword: function (e) {
        this.setData({confirm_password: e.detail.value.trim()})
    },
    /**
     * 获取输入车牌号
     */
    getCarNumber: function (e) {
        this.setData({carNumber: e.detail.value.trim()})
    },
    /**
     * 获取输入手机号
     */
    getTel: function (e) {
        this.setData({tel: e.detail.value.trim()})
    },
    /**
     * 获取输入车型
     */
    getStationType: function (e) {
        this.setData({stationType: e.detail.value})
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.onShow()
        wx.stopPullDownRefresh()
    }
})
